package org.openmore.consumer.controller.api;

import io.swagger.annotations.*;
import org.openmore.consumer.controller.common.BaseController;
import org.openmore.consumer.dto.common.BaseResponse;
import org.openmore.consumer.dto.common.DictionaryDto;
import org.openmore.consumer.service.feign.DictionaryService;
import org.openmore.consumer.annotation.TokenAuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@Api(value = "/Dictionary", tags = "Dictionary", description = "账号相关接口")
@RequestMapping(value = "/api/dictionary", produces = {APPLICATION_JSON_UTF8_VALUE})
@RestController
public class DictionaryController extends BaseController {

    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation(value = "分页返回所有菜单数据", response = DictionaryDto.class, responseContainer = "List")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的Activity", response = DictionaryDto.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/queryMenu")
    public BaseResponse selectMenuByPage(
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "菜单类型：first一级菜单，second二级菜单") String type,
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "菜单名") String name,
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "分页第几页") Integer pageNum,
            @RequestParam(required = false, defaultValue = "50") @ApiParam(value = "每页多少记录") Integer pageSize) {
        logger.debug(">> selectMenuByPage");
        return dictionaryService.selectMenuByPage(type, name, pageNum, pageSize);
    }


    @ApiOperation(value = "分页返回所有数据", response = DictionaryDto.class, responseContainer = "List")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的Activity", response = DictionaryDto.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    public BaseResponse selectAllByPage(
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "词典类型：public公共，private私有，all全部") String type,
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "词典名") String name,
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "分页第几页") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页多少记录") Integer pageSize) {
        logger.debug(">> selectAllByPage");
        return dictionaryService.selectAllByPage(type, name, pageNum, pageSize);
    }

    @ApiOperation(value = "根据父词典返回树结构返回列表", response = DictionaryDto.class, responseContainer = "List")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/query/tree")
    public BaseResponse selectTreeByParentKey(
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "父词典key，为空返回所有") String parentKey) {
        logger.debug(">> selectTreeByParentKey");
        return dictionaryService.selectTreeByParentKey(parentKey);
    }

    @ApiOperation(value = "根据父类id分页返回列表", response = DictionaryDto.class, responseContainer = "List")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的Activity", response = DictionaryDto.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/query/page")
    public BaseResponse selectAllByParentKeyPage(
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "词典名") String parentKey,
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "type级别:一级,二级，表示下级") String typeLevel,
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "名称") String name,
            @RequestParam(required = false, defaultValue = "") @ApiParam(value = "排序") String sort,
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "分页第几页") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页多少记录") Integer pageSize) {
        logger.debug(">> selectAllByPage");
        return dictionaryService.selectAllByParentKeyPage(parentKey, typeLevel, name, sort, pageNum, pageSize);
    }

    @ApiOperation(value = "根据id获取Dictionary信息", response = DictionaryDto.class)
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的Dictionary", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public BaseResponse getDictionaryById(@PathVariable @ApiParam(required = true, value = "词典id") String id) {
        logger.debug(">> getDictionaryById");
        return dictionaryService.getDictionaryById(id);
    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation("更新")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：更新的数据不存在", response = BaseResponse.class),
            @ApiResponse(code = 400, message = "请求失败：数据更新失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.PUT)
    public BaseResponse updateDictionary(@RequestBody @ApiParam(value = "新Dictionary信息", required = true) DictionaryDto dictionary) {
        logger.debug(">> updateDictionary");

        return dictionaryService.updateDictionary(dictionary);
    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation("创建")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.POST, consumes = {APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse insertDictionary(@RequestBody @ApiParam(value = "创建Dictionary", required = true) DictionaryDto dictionary) {
        logger.debug(">> insertDictionary");
        return dictionaryService.insertDictionary(dictionary);
    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation(value = "删除指定Dictionary")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据删除失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public BaseResponse deleteDictionary(@PathVariable @ApiParam(value = "id", required = true) String id) {
        logger.debug(">> deleteDictionary");
        return dictionaryService.deleteDictionary(id);
    }

    @ApiOperation(value = "根据租户id获得指定类型下的所有子结点")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据删除失败", response = DictionaryDto.class, responseContainer = "List")})
    @RequestMapping(method = RequestMethod.GET, value = "/children/{parentKey}")
    public BaseResponse getChildrenByParentKey2(@PathVariable @ApiParam(value = "parentKey", required = true) String parentKey,
                                                                     @RequestParam(required = false, defaultValue = "false")  @ApiParam(value = "是否使用缓存", defaultValue = "false") Boolean cache) {
        logger.debug(">> getChildrenByParentKey2");
        return dictionaryService.getChildrenByParentKey2(parentKey, cache);
    }
}
